//
//  ShareHelp.m
//  Onskysell
//
//  Created by cg on 17/3/20.
//  Copyright © 2017年 cg. All rights reserved.
//

#import "ShareHelp.h"
#import "CGLodingView.h"
#import <UShareUI/UShareUI.h>

@implementation ShareHelp

+ (ShareHelp *)shared{
    static ShareHelp *help = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        help = [[ShareHelp alloc]init];
    });
    return help;
}

-(void)shareWithTitle:(NSString *)title content:(NSString *)content image:(id)img url:(NSString *)urlStr currentController:(UIViewController *)currentController fromType:(ShareFromType)fromType{
    //友盟分享部分~! ====================================

     //判断是否安装对应平台app(twitter和vk sdk不用安装app也能分享, 而linkedin和facebook要装app,这里避免苹果审核被拒)
    NSMutableArray *platforms = NSMutableArray.new;
    if ([self isInstallAppWithType:UMSocialPlatformType_WechatSession]) {
        [platforms addObject:@(UMSocialPlatformType_WechatSession)];
        [platforms addObject:@(UMSocialPlatformType_WechatTimeLine)];
    }
    if ([self isInstallAppWithType:UMSocialPlatformType_QQ]) [platforms addObject:@(UMSocialPlatformType_QQ)];
    
//     [platforms addObject:@(UMSocialPlatformType_Sina)];//暂时不要新浪了~!!!
    
     //    NSArray *platforms = @[@(UMSocialPlatformType_Linkedin),@(UMSocialPlatformType_VKontakte),@(UMSocialPlatformType_Facebook),@(UMSocialPlatformType_Twitter)];
     
     [UMSocialUIManager setPreDefinePlatforms:platforms];//,@(UMSocialPlatformType_GooglePlus)
     
     __block id shareImg;
     [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
         
         if (fromType == FromScreenshot) {//截屏分享
             NSString *screenshotUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"screenshotUrl"];
             if (screenshotUrl) {
             shareImg = screenshotUrl;
             [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"screenshotUrl"];
             }
         }
         else shareImg = img;
         
         //微信朋友圈只显示一个,就用标题
         UMShareWebpageObject *obj = [UMShareWebpageObject shareObjectWithTitle:title descr:platformType == UMSocialPlatformType_WechatTimeLine?title:content thumImage:shareImg];
         obj.webpageUrl = urlStr;
         
         UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
         messageObject.shareObject = obj;
         
         [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentController completion:^(id result, NSError *error) {
         
         if (error) {
         //                 UMSocialLogError(@"share error - %@", userInfo);
             [LICGToast showWithText:@"分享失败" bottomOffset:ToastBottomOffset];
         }else{
             [LICGToast showWithText:@"分享成功" bottomOffset:ToastBottomOffset];
         //                UMSocialLogError(@"share success result - %@", result);
         }
         }];
     }];
}

- (BOOL)isInstallAppWithType:(UMSocialPlatformType)type
{
    if ([[UMSocialManager defaultManager] isInstall:type]) return YES;
    return NO;
}

@end
